using System.Security.Cryptography;

namespace StudentVideo.Client.Utils;

public static class Tools {

   /// <summary>
   /// 生成随机的id
   /// </summary>
   /// <param name="length"></param>
   /// <returns></returns>
   public static string GenId(int length = 8) {
      const string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      const string alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

      var id = new char[length];

      // 第一个字符必须是字母
      id[0] = letters[RandomNumberGenerator.GetInt32(letters.Length)];

      // 剩余字符可以是字母或数字
      for (int i = 1; i < length; i++) {
         id[i] = alphanumeric[RandomNumberGenerator.GetInt32(alphanumeric.Length)];
      }

      return new string(id);
   }

}
